package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;
import android.view.View;

import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenCreateHiddenActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenInfoActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenUploadHiddenActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenUploadZeroActivity;
import com.lemon.smartsafetynew.ui.adapter.hidden.SafeHiddenListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.hidden.SafeHiddenUtils;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 隐患列表页
 * 2.4.2之后，此页面被抛弃，不再需要此页面
 * Created by Andrew on 2017/9/7.
 */

@Deprecated
public class SafeHiddenListFragment extends BasePullToRefreshFragment<SafeHiddenDownloadVo, List<SafeHiddenDownloadVo>>
        implements RecyclerTClickListener<SafeHiddenDownloadVo> {

    // ============== UI ====================
    private FloatingActionsMenu rightLabels;

    /**
     * [tab_type, list_type]
     * [0, 0]  新建
     * [0, 1]  整改
     * [0, 2]  复查
     * [1, 0]  全部
     * [1, 1]  未上报
     * [1, 2]  已上报
     */
    private int tab_type = 0;
    private int list_type = 0;

    private String status = "";


    /**
     * @param tab_type
     * @param list_type
     * @return
     */
    public static SafeHiddenListFragment newInstance(int tab_type, int list_type) {

        Bundle args = new Bundle();
        args.putInt(KeyConstants.KEY_SAFE_HIDDEN_TAB_TYPE, tab_type);
        args.putInt(KeyConstants.KEY_SAFE_HIDDEN_LIST_TYPE, list_type);

        SafeHiddenListFragment fragment = new SafeHiddenListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            tab_type = bundle.getInt(KeyConstants.KEY_SAFE_HIDDEN_TAB_TYPE);
            list_type = bundle.getInt(KeyConstants.KEY_SAFE_HIDDEN_LIST_TYPE);
            status = SafeHiddenUtils.getStatus(tab_type, list_type);
        }
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("up_to_official", "");
        if(UserTypeUtil.isGovType(AccountUtil.getUserType())) {   //政府人员
            params.put("company_id", "");
        } else {                                                  //企业人员
            params.put("company_id", AccountUtil.getCompanyId());
        }
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        //Log.e("yxy", "params = " + params);

        SafeHiddenServices
                .init()
                .getSafeHiddenList(RefreshType.REFRESH, params, status);
    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("up_to_official", "");
        if(UserTypeUtil.isGovType(AccountUtil.getUserType())) {   //政府人员
            params.put("company_id", "");
        } else {                                                  //企业人员
            params.put("company_id", AccountUtil.getCompanyId());
        }
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        //Log.e("yxy", "params = " + params);

        SafeHiddenServices
                .init()
                .getSafeHiddenList(RefreshType.LOAD_MORE, params, status);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<SafeHiddenDownloadVo> getLemonAdapter() {
        return new SafeHiddenListAdapter(mDataList, this, status);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_SAFE_HIDDEN_LIST + status;
    }

    @Override
    public void onItemClick(SafeHiddenDownloadVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, data);
        UIHelper.showActivity(getmContext(), bundle, SafeHiddenInfoActivity.class);
    }

    @Override
    protected void initViews() {
        super.initViews();

        //delete by Andrew.yin begin 我们这里不需要上报隐患，每个隐患都是上报到安监的
        //initFloatBtn();
        //initCommon();
        //delete by Andrew.yin end   我们这里不需要上报隐患，每个隐患都是上报到安监的

    }

    private void initFloatBtn() {

        // =============== FloatingActionsMenu =================
        rightLabels = (FloatingActionsMenu) rootView.findViewById(R.id.right_labels);

        FloatingActionButton zeroUploadBtn = new FloatingActionButton(getmContext());
        zeroUploadBtn.setSize(FloatingActionButton.SIZE_MINI);
        zeroUploadBtn.setIcon(R.mipmap.icon_hidden_upload_zero);
        zeroUploadBtn.setColorNormalResId(R.color.colorPrimary);
        zeroUploadBtn.setColorPressedResId(R.color.colorPrimaryDark);
        //rightLabels.addButton(zeroUploadBtn);
        zeroUploadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                UIHelper.showActivity(getmContext(), SafeHiddenUploadZeroActivity.class);
                rightLabels.collapse();
            }
        });

        FloatingActionButton hiddenUploadBtn = new FloatingActionButton(getmContext());
        hiddenUploadBtn.setSize(FloatingActionButton.SIZE_MINI);
        hiddenUploadBtn.setIcon(R.mipmap.icon_hidden_upload);
        hiddenUploadBtn.setColorNormalResId(R.color.colorPrimary);
        hiddenUploadBtn.setColorPressedResId(R.color.colorPrimaryDark);
        rightLabels.addButton(hiddenUploadBtn);
        hiddenUploadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                UIHelper.showActivity(getmContext(), SafeHiddenUploadHiddenActivity.class);
                rightLabels.collapse();
            }
        });

        FloatingActionButton newHiddenBtn = new FloatingActionButton(getmContext());
        newHiddenBtn.setSize(FloatingActionButton.SIZE_MINI);
        newHiddenBtn.setIcon(R.mipmap.icon_hidden_create_new);
        newHiddenBtn.setColorNormalResId(R.color.colorPrimary);
        newHiddenBtn.setColorPressedResId(R.color.colorPrimaryDark);
        rightLabels.addButton(newHiddenBtn);
        newHiddenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                UIHelper.showActivity(getmContext(), SafeHiddenCreateHiddenActivity.class);
                rightLabels.collapse();
            }
        });

    }

    /**
     * 公用控件初始化
     */
    private void initCommon() {
        rightLabels.setVisibility(View.VISIBLE);
    }

    /**
     * 下拉刷新
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ViewEvent event) {
        if (SafeHiddenInfoActivity.class.getName().equals(event.getTag())) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            mPullToRefreshRecyclerView.beginRefreshing();
        } else if(SafeHiddenCreateHiddenActivity.class.getName().equals(event.getTag())) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            mPullToRefreshRecyclerView.beginRefreshing();
        } else if(SafeHiddenUploadHiddenActivity.class.getName().equals(event.getTag())) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            mPullToRefreshRecyclerView.beginRefreshing();
        }
    }

}
